#!/bin/bash
#set -x
# 开启 CONFIG_DEBUG 环境变量输出日志信息
ZSH_HOME_ENV=${HOME}/.zshrc
function DEBUG(){
    msg=$1
    if [ ${CONFIG_DEBUG}=="ON" ];then
        now=`date +'%Y-%m-%d %H:%M:%S'`
        printf "[\033[32m%s DEBUG:] \033[0m \033[31m"%s"\033[0m \n" "${now}" ${msg}
    fi
}

function download_plugin(){
    plugin=$1
    dest=${HOME}/.oh-my-zsh/plugins/${plugin}
    if [ ! -d ${dest} ];then
        git clone https://github.com/zsh-users/${plugin}.git ${dest}
    fi

}
function change_zsh(){
    env=$1
    zsh_home=$2
    if [ -z ${zsh_home} ];then
        zsh_home=${ZSH_HOME_ENV}
    fi
    is_exist=`grep ${env} ${zsh_home}`
    if [ ! -z ${is_exist} ];then
        sed -i "a\${env}" ${zsh_home}
    fi
}
function download_zsh_plugin(){
    for plugin in $@;do
        DEBUG ${plugin}
        dst=${HOME}/.oh-my-zsh/plugins/${plugin}
        if [ ! -d ${dst} ];then
            DEBUG "Download ${dst}"
            git clone https://github.com/zsh-users/${plugin}.git ${dst}
        fi
        if [ "${plugin}" == "antigen" ];then

            setted_antigen=`grep "antigen use oh-my-zsh" ${ZSH_HOME_ENV}`
            if [ -z "${setted_antigen}" ];then
                sed -i "\$a\if [[ -e \${HOME}/.oh-my-zsh/plugins/antigen/antigen.zsh ]]; then\n    source \${HOME}/.oh-my-zsh/plugins/antigen/antigen.zsh\n antigen use oh-my-zsh\nfi" ${ZSH_HOME_ENV}
            fi
        fi
        is_plugin=`grep ${plugin} ${ZSH_HOME_ENV}`
        if [ -z "${is_plugin}" ];then
            sed -i "\$a\antigen bundle zsh-users/${plugin}" ${ZSH_HOME_ENV}
        fi
    done
}
function download_tool(){
    program=$1
    os_distribute=`cat /etc/lsb-release|grep "DISTRIB_DESCRIPTION"|grep -i -o "manjaro"|tr "[:upper:]" "[:lower:]"`
    can_find_it=`which ${program}`
    if [ -z ${can_find_it} ];then
        case ${os_distribute} in
            manjaro)
                sudo pacman -S ${program}
                ;;
            ubuntu)
                sudo apt install ${program}
                ;;
            debian)
                sudo apt install ${program}
                ;;
            ::)
                ;;
        esac
    fi
}
function install_zsh(){
    download_tool curl
    download_tool zsh
    if [ -f ${ZSH_HOME_ENV} ];then
        cp ${ZSH_HOME_ENV} .
        DEBUG "备份原始.zshrc文件到当前目录"
    fi
    sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
    if [ $? ];then
        bash install.sh
    fi
}
function zsh_for_os(){
    os_distribute=`cat /etc/lsb-release|grep "DISTRIB_DESCRIPTION"|grep -i -o "manjaro"|tr "[:upper:]" "[:lower:]"`
    case ${os_distribute} in
        manjaro)
            export CONFIG_DEBUG=ON
            DEBUG ${os_distribute}
            ;;
        ubuntu)
            DEBUG ${os_distribute}
            install_zsh
            ;;
    
        *)
            printf "hello world"
            ;;
    esac
}
function set_theme(){
    theme=$1
    is_set_theme=`grep -nR "antigen theme" ${ZSH_HOME_ENV}`
    if [ -z "${is_set_theme}" ];then
        sed -i "\$a\antigen theme ${theme}" ${ZSH_HOME_ENV}
    else
        DEBUG ${theme}
        sed -i "s/antigen theme.*/antigen theme ${theme}/g" ${ZSH_HOME_ENV}
    fi
    is_on=`grep -nR "antigen apply" ${ZSH_HOME_ENV}`
    if [ -z "${is_on}" ];then
        sed -i "\$aantigen apply" ${ZSH_HOME_ENV}
    fi




}
download_zsh_plugin antigen zsh-autosuggestions zsh-syntax-highlighting zsh-completions zsh-history-substring-search
set_theme robbyrussell
